import {
	mapState,
	mapMutations
} from 'vuex'
import {
	getScanCodeGoodsInfo,
	getScanCodeGoods,
	addScanCodeGoods,
} from '@/api/index.js'

// #ifdef APP-PLUS
const mpaasScanModule = uni.requireNativePlugin("Mpaas-Scan-Module")
// #endif

export default {
	// #ifdef APP-PLUS
	async onShow() {
		this.handleScanResult()
	},
	onUnload() {
		this.setProductCode('')
	},
	// #endif
	data() {
		return {
			product: {
				goodsSn: '',
				goodsName: '',
				price: '',
				priceOriginal: '',
				thumb: '',
				maxBugCount: 0,
			}
		}
	},
	computed: {
		...mapState(['productCode']),
	},
	methods: {
		...mapMutations(['setProductCode']),
		openScan() {
			// #ifdef MP-WEIXIN
			uni.scanCode({
				success: (res) => {
					console.log('uni.scanCode success...', res)
					this.setProductCode(res.resp_result)
					this.handleScanResult()
				},
				fail: () => {
					console.log('uni.scanCode fail...')
				}
			})
			// #endif

			// #ifdef APP-PLUS
			// uni.navigateTo({
			// 	animationType: 'none',
			// 	url: '/pages/scanCode/scanCode'
			// })
			mpaasScanModule.mpaasScan({
				'type': 'qr', // 扫码 UI 风格，参数可为 qr、bar，默认为 qr
				'scanType':  ['qrCode','barCode'], // 扫码识别类型，参数可多选，qrCode、barCode，不设置，默认识别所有
				'hideAlbum': false // 是否隐藏相册，默认false不隐藏
			},
			(res) => {
				if (res.resp_code == 1000) {
					console.log('uni.scanCode success...', res)
					this.setProductCode(res.resp_result)
					this.handleScanResult()
				} else if (res.resp_code == 10) {
					console.log('用户取消了...')
				} else {
					console.log('扫码出错了...')
				}
			})
			// #endif
		},
		async handleScanResult() {
			if (this.productCode) {
				const res = await getScanCodeGoodsInfo(this.productCode)
				if (res) {
					this.product = res
					this.$refs.popup.open()
				} else {
					uni.showToast({
						title: '暂无商品信息',
						duration: 3000,
						icon: 'none'
					});
				}
			}
		},
		close(done) {
			// 关闭对话框
			this.setProductCode('')
			done()
		},
		async confirm(done, quantity) {
			console.log('quantity---', quantity)
			// TODO 调用加入购物车 api ，将api返回的数据提交到vuex，重定向到购物车页面，
			const res = await addScanCodeGoods(this.product.goodsSn, this.productCode, quantity)
			console.log('confirm---', res)
			this.setProductCode('')
			const pages = getCurrentPages();
			const route = pages[pages.length - 1].route;
			console.log('route---', route)
			if (route == 'pages/scan/scan') {
				uni.redirectTo({
					url: '/pages/scanCodeCart/scanCodeCart'
				})
			} else {
				const res = await getScanCodeGoods()
				console.log('getScanCodeGoods---', res)
				this.setData(res)
			}
			done()
		},
		setData(res) {
			// 给某些 cartItem 添加 withHeader 和 promotions 属性
			this.dataList = res.items.map((e, i) => {
				if (e.promotions.length) {
					e.goodsList[0].withHeader = true
					e.goodsList[0].promotions = e.promotions
				}
				return e.goodsList
			})
			.reduce((x, y) => x.concat(y))
			.map(e => {
				e.selected = false
				return e
			})
			// 给所有 cartItem 添加 selected 属性
			// this.dataList.forEach(e => e.selected = false)
			
			this.totalPrice = res.totalPrice
			this.totalPriceOriginal = res.totalPriceOriginal
			this.saving = res.saving
		}
	}
}
